/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package objectRank;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.fiu.cis.personalize.shared.GoogleDocument;

/**
 *
 * @author Reza
 */
public class DocumentServlet extends HttpServlet {

    //GraphBean gb = new GraphBean();
    Map<String, String[]> parMap;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        parMap = request.getParameterMap();
        String[] query = parMap.get("query");
        String[] documents = parMap.get("document");
        String[] titles = parMap.get("title");
        //String[] contents = parMap.get("content");
        System.out.println("Length of documents: >>>>>>>>>" + titles.length);
        //gb.addGoogleDocs(documents, titles, contents);
        GoogleDocument doc;
        LinkedList<GoogleDocument> googleDocuments = new LinkedList<GoogleDocument>();
        for(int i = 0 ; i < titles.length ; i++){
            doc = new GoogleDocument(documents[i], titles[i]);
            googleDocuments.add(doc);
        }
        System.out.println("printed to variable.");
        File file = new File("C:\\Users\\Reza\\Documents\\NetBeansProjects\\MyGroups"
                   + "\\src\\java\\objectRank\\tempDoc.bin");
        ObjectOutputStream objectOut = null;
        file.delete();
        file.createNewFile();
        try{
            objectOut = new ObjectOutputStream(new BufferedOutputStream(
                        new FileOutputStream(file)));
            objectOut.writeObject(googleDocuments);
            System.out.println("variable is printed to file");
        }catch(IOException e){
            System.err.println("Error: " + e);
        }

        try {
            objectOut.close(); // Close the output stream
        }catch(IOException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }
        System.out.println("docs are added to googledocs.");
    
    }

    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
